package com.zmn.plat.services.impl.customer;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.user.Customer;
import com.zmn.plat.model.entity.user.CustomerQuery;
import com.zmn.plat.persistence.interfaces.user.CustomerDao;
import com.zmn.plat.services.interfaces.user.CustomerService;

/**
 * Customer: nowind
 * 
 * @since 18/04/18 20:18
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerDao customerDao;

    @Override
    public Customer findByKey(Integer customerId) {
        return customerDao.findByKey(customerId);
    }

    @Override
    public List<Customer> listPageByQuery(CustomerQuery query) {
        Integer count = this.countByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : customerDao.listPageByQuery(query);
    }

    @Override
    public List<Customer> listByQuery(CustomerQuery query) {
        return customerDao.listByQuery(query);
    }

    @Override
    public Integer countByQuery(CustomerQuery query) {
        return customerDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(Customer customer) {
        if (customer.getUpdateTime() == null) {
            customer.setUpdateTime(DateUtil.getNow());
        }
        return customerDao.updateByKey(customer);
    }

    @Override
    public Integer updateStatus(Integer customerId, Integer status, String updater) {
        Customer customer = new Customer();
        customer.setCustomerId(customerId);
        customer.setStatus(status);
        customer.setUpdater(updater);
        return this.updateStatus(customer);
    }

    @Override
    public Integer updateStatus(Customer customer) {
        if (customer.getUpdateTime() == null) {
            customer.setUpdateTime(DateUtil.getNow());
        }
        return customerDao.updateStatus(customer);
    }

    @Override
    public Integer updateStatusBatch(List<Integer> customerIds, Integer status, String updater) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(customerIds);
        dto.setStatus(status);
        dto.setUpdater(updater);
        return this.updateStatusBatch(dto);
    }

    @Override
    public Integer updateStatusBatch(StatusDTO dto) {
        if (dto.getUpdateTime() == null) {
            dto.setUpdateTime(DateUtil.getNow());
        }
        return customerDao.updateStatusBatch(dto);
    }

    @Override
    public Integer insert(Customer customer) {

        if (customer.getUpdateTime() == null) {
            customer.setUpdateTime(DateUtil.getNow());
        }
        customer.setCreater(customer.getUpdater());
        customer.setCreateTime(customer.getUpdateTime());

        return customerDao.insert(customer);
    }

    @Override
    public Integer deleteByKey(Integer customerId) {
        return customerDao.deleteByKey(customerId);
    }

}
